package com.yksj.healthtalk.ui.friend;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import android.content.Intent;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ImageView;
import android.widget.ListView;

import com.yksj.healthtalk.adapter.InfoAdapter;
import com.yksj.healthtalk.comm.BaseFragmentActivity;
import com.yksj.healthtalk.entity.ChooseTagsEntity;
import com.yksj.healthtalk.entity.CustomerInfoEntity;
import com.yksj.healthtalk.ui.R;
import com.yksj.healthtalk.ui.doctor.DoctorSearchResultActivity;
import com.yksj.healthtalk.ui.views.PullToRefreshListView;
import com.yksj.healthtalk.utils.FriendUtil;
import com.yksj.healthtalk.utils.XMLUtils;

/**
 * @author Administrator
 *相同经历查找
 */
public class FriendSearchAboutExperienceActivity extends BaseFragmentActivity implements OnItemClickListener,OnClickListener{
	private ListView mListView;
	private InfoAdapter adapter;
	String[] datas;
	private ArrayList<HashMap<String, Object>> data = new ArrayList<HashMap<String, Object>>();
	private boolean isAge;
	private CustomerInfoEntity mCustomerInfoEntity;
	private ArrayList<String> ids ;
	private ArrayList<String> names; 
	private Boolean isFromMyPatient = false;//是否是我的患者中的搜素
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN);
		setContentView(R.layout.friend_search_aboutexperience);
		
		initWidget();
		initData();
	}
	
	private void initData() {
			titleRightBtn2.setVisibility(View.VISIBLE);
			titleRightBtn2.setText(R.string.sure);
			titleTextV.setText("按健康关注");
			
			if(getIntent().hasExtra("search_type") && getIntent().getExtras().getInt("search_type") == 0 ){
				isFromMyPatient = true;
			}
		
			//获取对象
			mCustomerInfoEntity = (CustomerInfoEntity) getIntent().getExtras().get("mCustomerInfoEntity");
			mCustomerInfoEntity.setType(2);
			List<ChooseTagsEntity> tags = XMLUtils.parseUserLablesTags(getResources(), R.xml.user_chooice_tags, 2);
			if (ids == null) {
				ids = new ArrayList<String>();
				names = new ArrayList<String>();
				ids.add("0");
				names.add("全部");
				for (ChooseTagsEntity tags2 : tags) {
					names.add(tags2.getName());
					ids.add(tags2.getId());
				}
			}
			datas = (String[]) names.toArray(new String[names.size()]);
			adapter = new InfoAdapter(getBaseContext(), data, true);
			mListView.setAdapter(adapter);
			mListView.setOnItemClickListener(this);
//			ArrayList<Integer> items = getIntent().getIntegerArrayListExtra("items");
			ArrayList<Integer> items = new ArrayList<Integer>();
			if (mCustomerInfoEntity.getSameExperienceCode() != 0) {
				items.add(Integer.valueOf(mCustomerInfoEntity.getSameExperience()));
			}else {
				items.add(0);
			}
			adapter.addSelectAll(items);
			setData(datas);
	}
	
	private void setData(String[] strs) {
		data.clear();
		for (int i = 0; i < strs.length; i++) {
			HashMap<String, Object> map = new HashMap<String, Object>();
			map.put("id", i);
			map.put("title", strs[i]);
			map.put("content", "");
			data.add(map);
		}
		adapter.notifyDataSetChanged();
	}

	/**
	 * 
	 */
	private void initWidget() {
		initTitle();
		PullToRefreshListView pullRefreshView = (PullToRefreshListView) findViewById(R.id.att_list);
		pullRefreshView.setLayoutInvisible();
		mListView = pullRefreshView.getRefreshableView();
		
		titleLeftBtn.setOnClickListener(this);
		titleRightBtn2.setOnClickListener(this);
		
		
	}

	/**
	 * listView 点击监听
	 */
	@Override
	public void onItemClick(AdapterView<?> parent, View view, int position,
			long arg3) {
		adapter.removeSelect(position);
		ImageView image = (ImageView) view.findViewById(R.id.att_icon);
			image.setVisibility(View.VISIBLE);
			adapter.addSelect(position);
		    adapter.notifyDataSetChanged();
	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		switch (v.getId()) {
		case R.id.title_back:
			setResult(-1);
			finish();
			break;
		case R.id.title_right2:
			if (isFromMyPatient) {
				Intent intent = new Intent(this, DoctorSearchResultActivity.class);
				intent.putExtra("mCustomerInfoEntity", mCustomerInfoEntity);
				startActivity(intent);
			}else {
				FriendUtil.setCustomerInfoEntityOthersDefault(mCustomerInfoEntity, 3);
				Intent intent = new Intent();
				mCustomerInfoEntity.setSameExperienceCode(Integer.valueOf(ids.get(adapter.getSelects().get(0))));
				mCustomerInfoEntity.setSameExperience(adapter.getSelects().get(0)+"");
				intent.putExtra("mCustomerInfoEntity",mCustomerInfoEntity);
				intent.setClass(this, FriendSearchMainActivity.class);
				setResult(0,intent);
				finish();
			}
			break;
		}
	}
	
	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if (keyCode == KeyEvent.KEYCODE_BACK) {
			setResult(-1);
			finish();
		}
		return super.onKeyDown(keyCode, event);
	}

}
